package com.zit.java.controller;


import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zit.java.pojo.Account;
import com.zit.java.pojo.SysMenu;
import com.zit.java.service.menu.MenuService;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("toMenu")
    public String toMenuPage() {
        return "menu/menu";
    }

    /**
     * 查询所有的数据
     *
     * @return
     */
    @RequestMapping("queryMenu")
    @ResponseBody
    public TableData queryAllData(@Param("page") Integer page,@Param("limit") Integer limit) {
        PageHelper.startPage(page,limit);
        List<SysMenu> td = menuService.queryAllMenu();
        PageInfo info = new PageInfo(td);
        TableData tableData = new TableData();
        tableData.setData(info.getList());
        tableData.setCount(info.getTotal());
        tableData.setCode(0);
        tableData.setMsg("ok");
        return tableData;
    }

    /**
     * 通过菜单名查询菜单无效
     * @return
     */
    @RequestMapping("queryMenuByMenu")
    @ResponseBody
    public TableData queryAllDataByMenu(@RequestParam("menuName") String menuName) {
        TableData tableData = new TableData();
        List<SysMenu> menuList =    menuService.queryMenuByMenuName(menuName);
        tableData.setData(menuList);
        tableData.setCode(0);
        return tableData;
    }

    /**
     * 到添加页面
     */
    @RequestMapping("toAddMenu")
    public String addMenu() {

        return "menu/addMenu";
    }


    /**
     * 添加数据到数据库
     *
     * @param sysMenu
     * @return
     */
    @ResponseBody
    @RequestMapping("addMenu")
    public AjaxDone saveMenu(SysMenu sysMenu, HttpServletRequest request) {
        Account loginAcc = BaseController.getLoginAcc(request);
        sysMenu.setCreateDate(DateUtil.now());
        sysMenu.setCreatUser(loginAcc.getName());

        int res = menuService.saveMenu(sysMenu);
        if (res > 0) {
            return new AjaxDone(200, "添加成功");
        } else {
            return new AjaxDone(300, "添加失败");
        }
    }

    @RequestMapping("delMenu")
    @ResponseBody
    public AjaxDone deleteMenu(int menuId) {
        int rst = menuService.deleteMenu(menuId);
        if (rst > 0) {
            return new AjaxDone(200, "删除成功");
        }
        return new AjaxDone(300, "删除失败!");
    }

    //    menu/deleteBatch
    @RequestMapping("deleteBatch")
    @ResponseBody
    public AjaxDone deleteMenuBatch(@RequestParam("menuIds[]") String [] menuIds) {

        int rst = menuService.deleteMenuBatch(menuIds);
        if (rst > 0) {
            return new AjaxDone(200, "删除成功");
        }
         return new AjaxDone(300, "删除失败!");
    }
    /**
     *
     *  2022-01-13未写
     * 到编辑页面
     */
//    @RequestMapping("updateMenu")
//    public String UpdateMenu(Integer menuId ,Model model){
//          SysMenu sysMenu  =      menuService.selectByPrimaryKey(menuId);
//        model.addAttribute("title",sysMenu );
//
//        return "menu/updMenu";
//    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @RequestMapping("updateMenu")
    @ResponseBody
    public AjaxDone updteMenu(SysMenu sysMenu) {
        int rst = menuService.updateMenu(sysMenu);
        if (rst > 0) {
            return new AjaxDone(200, "修改成功");
        }
        return new AjaxDone(300, "系统繁忙");
    }

    /**
     * 到编辑页面
     *
     * @return
     */
    @RequestMapping("toUpdate")
    public String toUpdate(Model model, Integer menuId) {
        SysMenu sysMenu = menuService.selectByMenuId(menuId);
        model.addAttribute("title", sysMenu);
        return "menu/updMenu";
    }


    /**
     * 导出excel
     */
    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            //创建工作簿
            HSSFWorkbook wb = new HSSFWorkbook();
            ///hssf是excel2003以前的
            //xssf是操作Excel2007的
            //创建 sheet 页
            HSSFSheet hssfSheet = wb.createSheet("菜单信息");
            //创建第一行的数据
            HSSFRow hssfRow = hssfSheet.createRow(0);
            //设置页面网格线是否显示
            hssfSheet.setDisplayGridlines(true);
            //设置单元格的宽度;
            hssfSheet.setColumnWidth(0, 200 * 30);
            hssfSheet.setColumnWidth(1, 200 * 30);
            hssfSheet.setColumnWidth(2, 200 * 30);
            hssfSheet.setColumnWidth(3, 200 * 30);
            hssfSheet.setColumnWidth(4, 200 * 30);
            hssfSheet.setColumnWidth(5, 200 * 30);
            hssfSheet.setColumnWidth(6, 200 * 30);
            hssfSheet.setColumnWidth(7, 200 * 30);
            hssfSheet.setColumnWidth(8, 200 * 30);
            //设置边框的样式
            CellStyle style = wb.createCellStyle();
            //标题水平居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //创建字体
            Font font = wb.createFont();
            //加粗
            font.setBold(true);
            //设置字体的高度
            font.setFontHeight((short) 200);
            //设置斜体
//        font.setItalic(true);
            //设置颜色
            Color color = new HSSFColor();
            font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());

            //设置字体的风格
            style.setFont(font);

            style.setBorderBottom(BorderStyle.MEDIUM);
            style.setBorderTop(BorderStyle.MEDIUM);
            style.setBorderRight(BorderStyle.MEDIUM);
            style.setBorderLeft(BorderStyle.MEDIUM);
            //创建单元格的值
            HSSFCell hssfCell = hssfRow.createCell(0);
            hssfCell.setCellValue("ID");
            hssfCell.setCellStyle(style);

            HSSFCell hssfCell1 = hssfRow.createCell(1);
            hssfCell1.setCellValue("标题");
            hssfCell1.setCellStyle(style);

            HSSFCell hssfCell2 = hssfRow.createCell(2);
            hssfCell2.setCellValue("地址");
            hssfCell2.setCellStyle(style);

            HSSFCell hssfCell3 = hssfRow.createCell(3);
            hssfCell3.setCellValue("目标");
            hssfCell3.setCellStyle(style);

            HSSFCell hssfCell4 = hssfRow.createCell(4);
            hssfCell4.setCellValue("菜单ID");
            hssfCell4.setCellStyle(style);

            HSSFCell hssfCell5 = hssfRow.createCell(5);
            hssfCell5.setCellValue("创建者");
            hssfCell5.setCellStyle(style);

            HSSFCell hssfCell6 = hssfRow.createCell(6);
            hssfCell6.setCellValue("创建时间");
            hssfCell6.setCellStyle(style);

            HSSFCell hssfCell7 = hssfRow.createCell(7);
            hssfCell7.setCellValue("创建者");
            hssfCell7.setCellStyle(style);

            HSSFCell hssfCell8 = hssfRow.createCell(8);
            hssfCell8.setCellValue("更新者");
            hssfCell8.setCellStyle(style);

            //1.取数据
            List<SysMenu> lsit = menuService.queryAllMenu();

            for (int i = 0; i < lsit.size(); i++) {
                hssfRow = hssfSheet.createRow(i + 1);
                hssfRow.setHeight((short) 250);

                HSSFCell hssfCell9 = hssfRow.createCell(0);

                hssfCell9.setCellValue(lsit.get(i).getMenuId().toString());

                HSSFCell hssfCell10 = hssfRow.createCell(1);
                hssfCell10.setCellValue(lsit.get(i).getTitle());

                HSSFCell hssfCell11 = hssfRow.createCell(2);
                hssfCell11.setCellValue(lsit.get(i).getHref());

                HSSFCell hssfCell12 = hssfRow.createCell(3);
                hssfCell12.setCellValue(lsit.get(i).getTarget());

                HSSFCell hssfCell13 = hssfRow.createCell(4);
                hssfCell13.setCellValue(lsit.get(i).getpMenuId());

//                HSSFCell hssfCell14 = hssfRow.createCell(5);
//                hssfCell14.setCellValue(lsit.get(i).getCreatUser());

//                HSSFCell hssfCell15 = hssfRow.createCell(6);
//                hssfCell15.setCellValue(lsit.get(i).getCreateDate());
//
//                HSSFCell hssfCell16 = hssfRow.createCell(7);
//                hssfCell16.setCellValue(lsit.get(i).getCreateDate());

            }
            //2.将数据放入到单元格
            OutputStream os = response.getOutputStream();
            wb.write(os);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping("exit")
    public String exitLogin(HttpSession session) {
        //清除session
        session.invalidate();
        //        /WEB-INF/pages/
        return "../../login";
    }

    /**
     * 到分步支付页面
     *
     * @return
     */

    @RequestMapping("toSetting")
    public String toSetting() {
        return "setting/fenbu";
    }

    /**
     * 到设置页面
     * @return
     */

    @RequestMapping("setting")
    public String setting(){
        return "setting/setting";
    }

    @RequestMapping("tomagess")
    public String toMagess(){
        return "menu/magesslist";
    }
}
